package net.fruit.mq.client.impl.util;

import java.util.Arrays;

import org.apache.rocketmq.client.log.ClientLogger;
import org.slf4j.Logger;

public class ClientLoggerUtil {
    private static final String CLIENT_LOG_ROOT = "mq.client.logRoot";
    private static final String CLIENT_LOG_FILEMAXINDEX = "mq.client.logFileMaxIndex";
    private static final String CLIENT_LOG_LEVEL = "mq.client.logLevel";
    private static final String[] levelArray = {"ERROR", "WARN", "INFO", "DEBUG"};

    public static Logger getClientLogger() {
        //Make sure
        String onsClientLogRoot = System.getProperty(CLIENT_LOG_ROOT, "${user.home}/logs");
        System.setProperty(ClientLogger.CLIENT_LOG_ROOT, onsClientLogRoot);
        String onsClientLogLevel = System.getProperty(CLIENT_LOG_LEVEL, "INFO").trim().toUpperCase();
        if (!Arrays.asList(levelArray).contains(onsClientLogLevel)) {
            onsClientLogLevel = "INFO";
        }
        System.setProperty(ClientLogger.CLIENT_LOG_LEVEL, onsClientLogLevel);
        String onsClientLogMaxIndex = System.getProperty(CLIENT_LOG_FILEMAXINDEX, "10").trim();
        try {
            int maxIndex = Integer.parseInt(onsClientLogMaxIndex);
            if (maxIndex <= 0 || maxIndex > 100) throw new NumberFormatException();
        } catch (NumberFormatException e) {
            onsClientLogMaxIndex = "10";
        }
        System.setProperty(ClientLogger.CLIENT_LOG_MAXINDEX, onsClientLogMaxIndex);

        return ClientLogger.getLog();
    }
}
